package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.domain.OrderSetting;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 预约设置实现
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    //批量添加
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                //检查这天是否存在数据(日期)
                long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                //对count进行判断
                if (count > 0) {
                    //数据已存在,执行更新
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                } else {
                    //数据不存在,执行添加
                    orderSettingMapper.add(orderSetting);
                }
            }
        }
    }

    //根据日期查询预约设置数据
    public List<Map> getOrderSettingByMonth(String date) {
        List<Map> month = orderSettingMapper.getOrderSettingByMonth(date);
        return month;
    }

    //根据日期修改可预约人数
    public void editOrderSettingByDate(OrderSetting orderSetting) {
        long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if(count > 0){
            //当前日期已经进行了预约设置，需要进行修改操作
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        }else{
            //当前日期没有进行预约设置，进行添加操作
            orderSettingMapper.add(orderSetting);
        }
    }
}
